#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
struct Student
{
	char name[21];
	int age;
	char address[51];
	char phone[14];
	char Email[51];
};
void addRecord(const string &filename)
{
	ofstream outfile(filename,ios::app|ios::binary);
	if(!outfile)
	{
		cout<<"无法打开文件进入写入"<<endl;
		exit(0);
	}
	Student student;
	cout<<"请输入姓名：";
	cin.ignore();
	cin.getline(student.name,21);
	while(true)
	{
		cout << "请输入年龄：";
		cin >> student.age;
		if(student.age>=0&&student.age<=200)
		{
			break;
		}
		cout << "年龄输入无效，请输入0到200之间的数字。"<<endl;
	}
	cout<<"请输入地址：";
	cin.ignore();
	cin.getline(student.address,51);
	cout<<"请输入电话：";
	cin.getline(student.phone,14);
	cout<<"请输入邮箱：";
	cin.getline(student.Email,51);
	outfile.write(reinterpret_cast<const char*>(&student), sizeof(Student));
    outfile.close();
    cout << "记录添加成功" << endl;
}
void displayRecord(const string &filename)
{
	ifstream file(filename,ios::binary);
	if(!file)
	{
		cout <<"数据文件不存在！"<<endl;
		exit(0);
	}
	Student s;
	int count=0;
	cout << "\n======= 所有学生记录 =======" << endl;
	while(file.read(reinterpret_cast<char*>(&s), sizeof(Student))) 
	{
        cout << "\n姓名: " << s.name << endl;
    	cout << "年龄: " << s.age << endl;
    	cout << "地址: " << s.address << endl;
   		cout << "电话: " << s.phone << endl;
    	cout << "Email: " << s.Email << endl;
        cout << "--------------------------" << endl;
        count++;
    }
    if(count == 0) {
        cout << "没有找到任何记录！" << endl;
    }
    file.close();
}
void changeRecord(const string &filename)
{
	fstream file(filename,ios::binary|ios::in|ios::out);
	if(!file)
	{
		cout <<"数据文件不存在！"<<endl;
		exit(0);
	}
	char target[21];
	cout << "请输入要修改的学生姓名：";
	cin.ignore();
	cin.getline(target,21);
	Student s;
	bool found=false;
	long pos;
	while(file.read(reinterpret_cast<char*>(&s), sizeof(Student)))
	{
		if(strcmp(s.name,target)==0)
		{
			found = true;
            break;
		}
		pos+=sizeof(Student);
	}
	if(found)
	{
		cout << "找到记录，开始修改..." << endl;
        cout << "请输入新的姓名: ";
        cin.getline(s.name,21);
        while(true)
		{
			cout << "请输入新的年龄：";
			cin >> s.age;
			if(s.age>=0&&s.age<=200)
			{
				break;
			}
			cout << "年龄输入无效，请输入0到200之间的数字。"<<endl;
		}
		cout<<"请输入新的地址：";
		cin.ignore();
		cin.getline(s.address,51);
		cout<<"请输入新的电话：";
		cin.getline(s.phone,14);
		cout<<"请输入新的邮箱：";
		cin.getline(s.Email,51);
		file.seekp(pos);
		file.write(reinterpret_cast<const char*>(&s), sizeof(Student));
    	cout << "记录修改成功" << endl;
	}
	else 
	{
        cout << "未找到指定学生的记录" << endl;
    }
    file.close();
}
void searchRecord(const string &filename)
{
	ifstream infile(filename,ios::binary);
	if(!infile)
	{
		cout <<"数据文件不存在！"<<endl;
		exit(0);
	}
	char target[21];
	cout << "请输入要查找的学生姓名：";
	cin.ignore();
	cin.getline(target,21);
	Student s;
	bool found=false;
	while(infile.read(reinterpret_cast<char*>(&s), sizeof(Student)))
	{
		if(strcmp(s.name,target)==0)
		{
			found = true;
			cout << "姓名: " << s.name << endl;
            cout << "年龄: " << s.age << endl;
            cout << "地址: " << s.address << endl;
            cout << "电话: " << s.phone << endl;
            cout << "邮箱: " << s.Email << endl;
            cout << "-------------------------" << endl;
            break;
		}
	}
	if(!found)
	{
		cout << "未找到指定学生的记录" << endl;
	}
	infile.close();
}
void deleteRecord(const string &filename)
{
	ifstream infile(filename,ios::binary);
	if(!infile)
	{
		cout <<"数据文件不存在！"<<endl;
		exit(0);
	}
	ofstream tempFile("temp.txt", ios::binary);
	char target[21];
	cout << "请输入要删除的学生姓名：";
	cin.ignore();
	cin.getline(target,21);
	Student s;
	bool found=false;
	while(infile.read(reinterpret_cast<char*>(&s), sizeof(Student)))
	{
		if(strcmp(s.name,target)==0)
		{
			found = true;
		}
		else 
		{
            tempFile.write(reinterpret_cast<char*>(&s), sizeof(Student));
        }
	}
	infile.close();
    tempFile.close();
    if(found) 
	{
        remove(filename.c_str());
        rename("temp.txt", filename.c_str());
        cout << "记录删除成功！" << endl;
    } 
	else 
	{
        remove("temp.txt");
        cout << "未找到该学生记录！" << endl;
    }
}
int main()
{
	string filename="student.txt";
	int choice;
	while(true)
	{
		cout << "学生通讯录管理系统\n";
        cout << "1. 向文件中增加记录\n";
        cout << "2. 显示文件中的所有记录\n";
        cout << "3. 修改任意一个记录\n";
        cout << "4. 按照姓名查找一个学生的记录\n";
        cout << "5. 删除某个学生的记录\n";
        cout << "6. 退出\n";
        cout << "请选择操作: ";
        cin >> choice;
        switch(choice)
        {
        	case 1:
        		addRecord(filename);
        		break;
        	case 2:
        		displayRecord(filename);
        		break;
        	case 3:
        		changeRecord(filename);
        		break;
        	case 4:
        		searchRecord(filename);
        		break;
        	case 5:
        		deleteRecord(filename);
        		break;
        	case 6:
        		return 0;
        	default:
                cout << "无效选择，请重新输入" << endl;
		}
	}
	return 0;
}